import {
	addAirline,
	deleteAirlineById,
	getAirlineById,
	getAirlineIdPorts,
	getAirlineListForPage,
	getAllAirLines,
	updateAirline
} from "../../services/quotationSupport/airline";
import Constants from '../../utils/Constants'
import {message} from "antd";

const R = require('ramda');


export default {
	namespace: 'airline',
	state: {
		page: {},
		list: [],
		airlineList: [],
        loadingPortList:[],
        destinationPortist:[],
		dataSource: {},
	},
	reducers: {
        /**
         * 刷新state
         *
         */
		refreshState(state, { payload }) {
			return { ...state, ...payload };
		}
	},
	effects: {
        /**
         * 分页查询航线
         *
         * @param call
         * @param put
         */
		*getAirlineListForPage({ payload }, { call, put }) {
			const res = yield call(getAirlineListForPage, { ...R.omit(['onSuccess', 'onFail'])(payload) });
			if (res && res.data.resultCode === "ok") {
				yield put({
					type: 'refreshState',
					payload: {
						list: res.data.content.list,
						page: res.data.content.page
					}
				});
			} else {
				if (payload.onFail) payload.onFail();
			}
		},

        /**
         * 新增航线
         *
         * @param call
         */
		*addAirline({ payload }, { call }) {
			const res = yield call(addAirline, { ...R.omit(['onSuccess', 'onFail'])(payload) });
			if (res && res.data.resultCode === "ok") {
				message.info("航线新增成功！");
				if (payload.onSuccess) payload.onSuccess();
			} else {
				if (payload.onFail) payload.onFail();
			}
		},

        /**
         * 删除航线
         *
         * @param payload
         * @param call
         * @param put
         */
		*deleteAirlineById({ payload }, { call }) {
			const res = yield call(deleteAirlineById, { ...R.omit(['onSuccess', 'onFail'])(payload) });
			if (res && res.data.resultCode === "ok") {
				message.info("航线删除成功！");
				if (payload.onSuccess) payload.onSuccess();
			} else {
				if (payload.onFail) payload.onFail();
			}
		},

        /**
         * 更新航线
         *
         * @param call
         * @param put
         * @param select
         */
		*updateAirline({ payload }, { call, put }) {
			const res = yield call(updateAirline, { ...R.omit(['onSuccess', 'onFail'])(payload) });
			if (res && res.data.resultCode === "ok") {
				message.info("航线更新成功！");
				if (payload.onSuccess) payload.onSuccess();
			} else {
				if (payload.onFail) payload.onFail();
			}
		},

        /**
         * 获取航线
         *
         * @param payload
         * @param call
         * @param put
         */
		*getAirlineById({ payload }, { call, put }) {
			if (payload.id) {
				const res = yield call(getAirlineById, { ...R.omit(['onSuccess', 'onFail'])(payload) });
				if (res && res.data.resultCode === "ok") {
					yield put({
						type: 'refreshState',
						payload: {
							dataSource: res.data.content
						}
					});
				} else {
					if (payload.onFail) payload.onFail();
				}
			} else {
				yield put({
					type: 'refreshState',
					payload: {
						dataSource: {}
					}
				});
			}
		},
		/**
         * 查询航线List
         *
         */
        *getAllAirLines({ payload },{ call, put }){
            const res = yield call(getAllAirLines, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if(res && res.data.resultCode === "ok"){
                yield put({
                    type: 'refreshState',
                    payload:{
                        airlineList: R.isNil(res.data.content.list) ? [] : res.data.content.list
                    }
                });
            } else {
                if(payload.onFail) payload.onFail();
            }
        },

        /**
		 * 查询航线下的港口
         * @param payload
         * @param call
         * @param put
         * @returns {IterableIterator<*>}
         */
        *getAirlineIdPorts({ payload },{ call, put }){
            const res = yield call(getAirlineIdPorts, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if(res && res.data.resultCode === "ok"){
                yield put({
                    type: 'refreshState',
                    payload:{
                        loadingPortList: R.isNil(res.data.content.loadingPort) ? [] : res.data.content.loadingPort,
                        destinationPortist: R.isNil(res.data.content.destinationPort) ? [] : res.data.content.destinationPort
                    }
                });
            } else {
                if(payload.onFail) payload.onFail();
            }
        }
	},
	subscriptions: {
        setup({ history, dispatch }) {
            return history.listen(({ pathname }) => {
                const menus = JSON.parse(sessionStorage.getItem(Constants.USER_MENU));
                if(menus && !menus.find(item=>item.includes(pathname)) && pathname != '/logout'){
                    document.location.href = "/notAuthorized"
                }
            });
        }
    }
};
